package net.xdclass.aspect;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.lock.LockInfo;
import com.baomidou.lock.LockTemplate;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.annotation.RepeatSubmit;
import net.xdclass.constant.RedisKey;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.exception.BizException;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.model.LoginUser;
import net.xdclass.service.RepeatTypeFactory;
import net.xdclass.util.CommonUtil;
import org.apache.ibatis.jdbc.Null;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * @author Allen
 * @version 1.0
 * @date 2022/11/21 18:17
 */
@Component
@Aspect
@Slf4j
public class RepeatSubmitAspect {

	@Resource
	private RedisTemplate redisTemplate;


	@Resource
	private LockTemplate lockTemplate;


	@Resource
	private RepeatTypeFactory repeatTypeFactory;


	@Pointcut("@annotation(repeatSubmit)")
	public void checkLoginStatus(RepeatSubmit repeatSubmit) {
	}

	@Around(value = "checkLoginStatus(repeatSubmit)", argNames = "proceedingJoinPoint,repeatSubmit")
	public Object handleControllerMethod(ProceedingJoinPoint proceedingJoinPoint, RepeatSubmit repeatSubmit) throws Throwable {

		log.info("前置通知");

		Boolean result = repeatTypeFactory.getResult(repeatSubmit.limitType(), proceedingJoinPoint);
		if (Boolean.FALSE.equals(result)) {
			return null;
		}

//		LoginUser loginUser = LoginInterceptor.threadLocal.get();
//		HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
//
//		String name = repeatSubmit.limitType().name();
//		long lockTime = repeatSubmit.lockTime();
//
//
//		if (name.equals(RepeatSubmit.Type.PARAMS.name())) {
//			log.info("参数防重复");
//			String ipAddr = CommonUtil.getIpAddr(request);
//
//			MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
//			Method method = signature.getMethod();
//			String className = method.getDeclaringClass().getName();
//
//			RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
//			RepeatSubmit.Type type = annotation.limitType();
//			long lockTime1 = annotation.lockTime();
//			log.info("反射获取的注解信息：{},{}",type.name(),lockTime1);
//
//
//			String key = StrUtil.format("shop:repeat-submit:{}:{}:{}:{}", ipAddr, className, method.getName(), loginUser.getAccountNo());
//			LockInfo lock = lockTemplate.lock(key, lockTime * 1000, 0L);
//			if (ObjectUtil.isNull(lock)) {
//				log.warn("重复提交请求");
//				return null;
//			}
//
//		}
//
//		if (name.equals(RepeatSubmit.Type.TOKEN.name())) {
//			log.info("token防重复");
//			String header = request.getHeader("request-token");
//			if (StrUtil.isBlank(header)) {
//				throw new BizException(BizCodeEnum.ORDER_CONFIRM_TOKEN_EQUAL_FAIL);
//			} else {
//				String key = String.format(RedisKey.SUBMIT_ORDER_TOKEN_KEY, loginUser.getAccountNo(), header);
//				Boolean delete = redisTemplate.delete(key);
//				if (Boolean.FALSE.equals(delete)) {
//
//					log.warn("重复提交的请求");
//					return null;
//				}
//			}
//
//		}

		Object proceed = proceedingJoinPoint.proceed();
		log.info("后置通知");
		return proceed;
	}

}
